<!--
  * lmabbe
  *
  * @Author:    lmabbe
  * @Date:      2025-06-26 02:18:52
  * @Copyright  lmabbe
-->
<template>
  <!---------- 查询表单form begin ----------->
  <div v-privilege="'customerInfo:query'" class="container">
    <div>
      <a-form size="small">
        <a-row class="smart-query-form-row">
          <a-form-item label="关键字查询" class="smart-query-form-item">
            <a-input style="width: 200px" v-model:value="queryForm.keywords" placeholder="可查条码号/物料号/物料名称等" />
          </a-form-item>
          <a-form-item label="归属客户" class="smart-query-form-item">
            <a-select v-model:value="queryForm.belongCustomerCode" :allowClear="true" :style="`width: 200px`" placeholder="请选择归属客户">
              <a-select-option v-for="item in props.customerData" :key="item.customerCode" :value="item.customerCode">
                {{ item.customerAbbreviation }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="归属供应商" class="smart-query-form-item">
            <a-select v-model:value="queryForm.belongSupplierCode" :allowClear="true" :style="`width: 200px`" placeholder="请选择归属供应商">
              <a-select-option v-for="item in props.supplierData" :key="item.supplierCode" :value="item.supplierCode">
                {{ item.supplierAbbreviation }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item label="批次号" class="smart-query-form-item">
            <a-input style="width: 200px" v-model:value="queryForm.batchNo" placeholder="批次号" />
          </a-form-item>
          <a-form-item label="创建时间" class="smart-query-form-item">
            <a-range-picker v-model:value="queryForm.createTime" :presets="defaultTimeRanges" style="width: 200px" @change="onChangeCreateTime" />
          </a-form-item>
          <a-form-item class="smart-query-form-item">
            <a-button type="primary" @click="onSearch">
              <template #icon>
                <SearchOutlined />
              </template>
              查询
            </a-button>
            <a-button @click="resetQuery" class="smart-margin-left10">
              <template #icon>
                <ReloadOutlined />
              </template>
              重置
            </a-button>
          </a-form-item>
        </a-row>
      </a-form>
    </div>
    <!---------- 查询表单form end ----------->

    <div>
      <!---------- 表格 begin ----------->
      <a-table
        size="small"
        :scroll="{ y: 450 }"
        :dataSource="tableData"
        :columns="columns"
        rowKey="materialBarcode"
        bordered
        :loading="tableLoading"
        :pagination="false"
      >
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.dataIndex === 'status'">
            <span>{{ $smartEnumPlugin.getDescByValue('BASIC_MATERIAL_BARCODE_STATUS_ENUM', text) }}</span>
          </template>

          <template v-if="column.dataIndex === 'action'">
            <div class="smart-table-operate">
              <a-button type="link" @click="rePrint(record)">重新打印</a-button>
              <!--              <a-button danger type="link" @click="onDelete(record)">作废</a-button>-->
            </div>
          </template>
        </template>
      </a-table>
      <!---------- 表格 end ----------->

      <div class="smart-query-table-page">
        <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total) => `共${total}条`"
        />
      </div>
    </div>
  </div>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';

  import { materialBarcodeApi } from '/@/api/business/basic/material/material-barcode-api.js';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const';
  import { defaultTimeRanges } from '/@/lib/default-time-ranges';

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '物料条码',
      dataIndex: 'materialBarcode',
      ellipsis: true,
      width: '130px',
      align: 'center',
    },
    {
      title: '上级条码',
      dataIndex: 'parentMaterialBarcode',
      ellipsis: true,
      width: '130px',
      align: 'center',
    },
    {
      title: '物料编码',
      dataIndex: 'materialCode',
      ellipsis: true,
      width: '130px',
      align: 'center',
    },
    {
      title: '旧物料编号',
      dataIndex: 'oldMaterialCode',
      ellipsis: true,
      width: '130px',
      align: 'center',
    },
    {
      title: '物料名称',
      dataIndex: 'materialName',
      ellipsis: true,
      width: '130px',
      align: 'center',
    },
    {
      title: '物料规格',
      dataIndex: 'materialSpec',
      ellipsis: true,
      width: '130px',
      align: 'center',
    },
    {
      title: '物料描述',
      dataIndex: 'materialDesc',
      ellipsis: true,
      width: '300px',
      align: 'center',
    },
    {
      title: '物料单位',
      dataIndex: 'materialUnit',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '归属客户',
      dataIndex: 'belongCustomerAbbreviation',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '供应商简称',
      dataIndex: 'belongSupplierAbbreviation',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '品牌',
      dataIndex: 'brand',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '批次号',
      dataIndex: 'batchNo',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: 'D/C',
      dataIndex: 'dc',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '初始数量',
      dataIndex: 'initNum',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '包装数',
      dataIndex: 'packageNum',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '条码数量',
      dataIndex: 'barcodeNum',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '可用数量',
      dataIndex: 'useNum',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '有效天数',
      dataIndex: 'validDay',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '生产日期',
      dataIndex: 'manufactureDate',
      ellipsis: true,
      width: '150px',
      align: 'center',
    },
    {
      title: '到期日期',
      dataIndex: 'expirationDate',
      ellipsis: true,
      width: '150px',
      align: 'center',
    },
    {
      title: '条码状态',
      dataIndex: 'status',
      ellipsis: true,
      width: '80px',
      align: 'center',
    },
    {
      title: '先进先出码',
      dataIndex: 'fastCode',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '原厂商条码',
      dataIndex: 'oriFacBarcode',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '禁用状态',
      dataIndex: 'disabledFlag',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '创建人',
      dataIndex: 'createUserName',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      ellipsis: true,
      width: '150px',
      align: 'center',
    },
    {
      title: '更新人',
      dataIndex: 'updateUserName',
      ellipsis: true,
      width: '100px',
      align: 'center',
    },
    {
      title: '更新时间',
      dataIndex: 'updateTime',
      ellipsis: true,
      width: '150px',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: '150px',
      align: 'center',
    },
  ]);

  const props = defineProps({
    customerData: {
      type: Array,
      default: () => [],
    },
    supplierData: {
      type: Array,
      default: () => [],
    },
  });

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    keywords: undefined, //关键字查询
    belongCustomerCode: undefined, //归属客户
    belongSupplierCode: undefined, //归属供应商
    batchNo: undefined, //批次号
    createTime: [], //创建时间
    createTimeBegin: undefined, //创建时间 开始
    createTimeEnd: undefined, //创建时间 结束
    pageNum: 1,
    pageSize: 20,
  };
  // 查询表单form
  const queryForm = reactive({ ...queryFormState });
  // 表格加载loading
  const tableLoading = ref(false);
  // 表格数据
  const tableData = ref([]);
  // 总数
  const total = ref(0);

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize;
    Object.assign(queryForm, queryFormState);
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 搜索
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true;
    try {
      let queryResult = await materialBarcodeApi.queryPage(queryForm);
      tableData.value = queryResult.data.list;
      total.value = queryResult.data.total;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  function onChangeCreateTime(dates, dateStrings) {
    console.log(dates);
    console.log(dateStrings);
    queryForm.createTimeBegin = dateStrings[0];
    queryForm.createTimeEnd = dateStrings[1];
  }

  const emit = defineEmits(['reprint']);

  // function customerAll() {
  //   customerInfoApi.customerAll().then((res) => {
  //     customerData.value = res.data;
  //   });
  // }
  //
  // function supplierAll() {
  //   supplierInfoApi.all().then((res) => {
  //     supplierData.value = res.data;
  //   });
  // }

  function rePrint(data) {
    emit('reprint', data);
  }

  onMounted(queryData);

  defineExpose({
    queryData,
  });
</script>
